/**
 * 1.通过id获取对象
 */
function $(id){
    return document.getElementById(id);
}
/**
 * 2.获取网页传入数据
 */
function getQuery(variable)
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){return pair[1];}
    }
    return(false);
}

window.onload=function (){
    fullTable(1)
}

function fullTable(NO){
    axios.get("/topic/cutByItem", {params:{
            pageNO:NO,
            channelId: getQuery("id"),
            name:$("topicName").value,
            auth:$("auth").value,
            startDate:$("startDate").value,
            endDate:$("endDate").value

        }}).then(
        resp=>{
            var list= resp.data.list;
            getChannel();
            $("channelId").value=getQuery("id");
            var info="";
            list.forEach(
                e=>{
                    info+=`<tr>
                    <td>${e.name}</td><td>${e.auth}</td>
                    <td>${e.count}</td><td>${e.date}</td>
                    <td>
                    <input type="button" value="查看回帖" onclick="findById(${e.id})">
                    </td></tr>`;
                    $("topicData").innerHTML=info;



                }
            )

            var str="";
            for (let i = 1; i <=resp.data.pages ; i++) {
              str+=`<a style="margin-left: 10px" href="javascript:fullTable('${i}')">${i}</a>`  ;
            }
            $("page").innerHTML=str;
        }
    )
}

/**
 *
 */
function findById(id){
    axios.get("/topic/findById",{
        params:{
            id
        }
    }).then(
        resp=>{
            var topic=  resp.data;
            var id=`${topic.id}`;
            // var name=$("channelDiv").value;
            location.href="/html/discuss.html?id="+id+"";
        }
    )
}

/**
 *进入添加页面
 */
function add(){
    axios.get("/user/getLogin").then(
        resp=>{
            if(resp.data==""){
             location.href="/html/login.html"
            }
            else {
                $("addDiv").style.visibility="visible";
                $('addDiv').style.width = document.documentElement.clientWidth + "px";
                $('addDiv').style.height = document.documentElement.clientHeight + "px";
            }

        }
    )

}




function submit(){
    axios.get("/topic/add",{
        params:{
            channelId:$("channelId").value,
            contents:$("contents").value
        }
    }).then(
        resp=>{
            if(resp.data=="ok"){
                $("addDiv").style.visibility="hidden";
                fullTable(getQuery("id"))
            }


        }
    )

}

function  getChannel(){
    axios.get("/discuss/getChannel").then(
        resp=>{
            $("channelDiv") .innerHTML=resp.data;
        }
    )
}